<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 2020-01-02
 * Time: 11:00
 */

namespace app\common\model;


class XkAdmin extends Common
{

    /**
     * 获取管理员列表
     * @param array $map
     */
    public static function getAdmins($map = ['a.isDel' => 0])
    {

        if (cache(self::CACHE_ADMIN_ADMIN_LIST)) {
            $list = cache(self::CACHE_ADMIN_ADMIN_LIST);
        } else {
            $list = self::alias('a')
                ->field('a.*,b.roleName')
                ->join('xk_admin_role b', 'a.roleID = b.id', 'left')
                ->where($map)
                ->order('a.id asc')
                ->select();
            object2array($list);
            cache(self::CACHE_ADMIN_ADMIN_LIST, $list);
        }
        return $list;
    }

    /**
     * 验证管理员账号密码是否正确
     * @param $adminName
     * @param $adminPwd
     */
    public static function checkNamePwd($adminName, $adminPwd)
    {
        $admin = self::alias('a')
            ->field('a.*,b.roleName')
            ->join('xk_admin_role b', 'a.roleID = b.id', 'left')
            ->where(['a.adminName' => $adminName, 'a.isDel' => 0])->find();
        if (!$admin) backError(lang('invalid admin'));
        md5($adminPwd) != $admin['adminPwd'] && backError(lang('password error'));
        session('adminInfo', $admin);
        backSuccess([], lang('log in successfully'));
    }

    /**
     * 修改管理员密码
     * @param $oldPwd
     * @param $newPwd
     */
    public static function changePwd($oldPwd, $newPwd)
    {
        $admin = self::where(['id' => session('adminInfo')['id'], 'isDel' => 0])->find();
        !$admin && backError(lang('invalid admin'));
        //  旧密码错误
        $admin['adminPwd'] !== md5($oldPwd) && backError(lang('password error'));

        $res = self::where(['id' => session('adminInfo')['id']])->save(['adminPwd' => md5($newPwd)]);
        $res ? backSuccess([], lang('edit successfully')) : backError(lang('edit failed'));
    }

    /**
     * 添加管理员账号
     * @param $adminName
     * @param $adminPwd
     * @param $roleID
     * @param string $adminAvatar
     */
    public static function addAdmin($adminName, $adminPwd, $roleID, $adminAvatar = '', $adminPhone = '', $adminEmail = '')
    {
        $res = self::add([
            'adminName' => $adminName,
            'adminPwd' => md5($adminPwd),
            'adminAvatar' => $adminAvatar,
            'roleID' => $roleID,
            'adminPhone' => $adminPhone,
            'adminEmail' => $adminEmail,
            'addTime' => time()
        ]);
        $res && clearCacheByKey(self::CACHE_ADMIN_ADMIN_LIST);
        $res ? backSuccess([], lang('create successfully')) : backError(lang('create failed'));
    }

    /**
     * 编辑管理员信息
     * @param $adminName
     * @param $roleID
     * @param $id
     * @param string $adminPwd
     * @param string $adminAvatar
     * @return array
     */
    public static function editAdmin($adminName, $roleID, $id, $adminPwd = '', $adminAvatar = '', $adminPhone = '', $adminEmail = '')
    {
        $saveData = [
            'adminName' => $adminName,
            'roleID' => $roleID,
            'updateTime' => time()
        ];
        $adminPwd && $saveData['adminPwd'] = md5($adminPwd);
        $adminAvatar && $saveData['adminAvatar'] = $adminAvatar;
        $adminPhone && $saveData['adminPhone'] = $adminPhone;
        $adminEmail && $saveData['adminEmail'] = $adminEmail;
        $res = self::where(['id' => $id])->save($saveData);
        $res && clearCacheByKey(self::CACHE_ADMIN_ADMIN_LIST);
        return $res ? ['status' => 1, 'msg' => lang('edit successfully')] : ['status' => 0, 'msg' => lang('edit failed')];
    }
}